// pages/shop_add/shop_add_img_manage/shop_add_img_manage.js
const api = require('../../../utils/util.js');
const service = require('../../../utils/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperImageArr: [],
    num: 8,
    swiperIndex: 0,
  },

  // onChange: function (e) {
  //   console.log(e.detail)
  // },

  //swiper指针改变事件
  goSettingCurrentIndex: function(e){
    this.setData({
      swiperIndex: e.detail.current
    })
  },

  //--打开添加图片页面（点击按钮）、
  chooseImage: function() {
    let _this = this;

    wx.showActionSheet({
      itemList: ['拍照模式', '手机相册'],
      success: function(res) {
        if (res.tapIndex == 0) {
          api.upLoadImg(this.data.num, 'camera', function(res) {
            console.log(res)
            let imgList = _this.data.swiperImageArr.slice();
            let num = _this.data.num;
            imgList.push(res)
            num--;

            _this.setData({
              swiperImageArr: imgList,
              num: num,
              swiperIndex: imgList.length - 1
            })
          })
        } else if (res.tapIndex == 1) {
          api.upLoadImg(_this.data.num, 'album', function(res) {
            let imgList = _this.data.swiperImageArr.slice();
            let num = _this.data.num;

            for (let i = 0; i < res.length; i++) {
              imgList.push(res[i])
            }
            num -= res.length;

            _this.setData({
              swiperImageArr: imgList,
              num: num,
              swiperIndex: imgList.length - 1
            })
            console.log(_this.data.num)
          })
        }
      }
    })
  },

  swiperSliderDel: function(e){
    let _this = this;
    wx.showModal({
      title: '',
      content: '删除该图片？',
      showCancel: true,
      confirmText: '删除',
      success: function(res) {
        if(res.confirm){
          let inds = e.currentTarget.dataset.delIndex;
          console.log(inds);
          let imgArr = _this.data.swiperImageArr.slice();
          let num = _this.data.num;
          let swiperIndex = _this.data.swiperIndex;
          imgArr.splice(inds, 1);
          num++;
          if (imgArr.length == inds){
            swiperIndex = inds - 1;
          }
          _this.setData({
            swiperImageArr: imgArr,
            num: num,
            swiperIndex: swiperIndex
          })
          if (num == 8) {
            wx.navigateBack({
              success: function(){
                var pages = getCurrentPages();
                var currPage = pages[pages.length - 1]; //当前页面
                var prevPage = pages[pages.length - 2]; //上一个页面

                //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
                prevPage.setData({
                  swiperImageArr: [],
                  num: 8,
                  swiperIndex: _this.data.swiperIndex
                })
                currPage.setData({
                  swiperImageArr: [],
                  num: 8,
                  swiperIndex: _this.data.swiperIndex
                })
              }
            })
          }
        }
      },
    })
  },

  save:function(){
    let _this = this;
    wx.navigateBack({
      success: function () {
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1]; //当前页面
        var prevPage = pages[pages.length - 2]; //上一个页面

        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPage.setData({
          swiperImageArr: _this.data.swiperImageArr,
          num: _this.data.num,
          swiperIndex: _this.data.swiperIndex
        })
        currPage.setData({
          swiperImageArr: _this.data.swiperImageArr,
          num: _this.data.num,
          swiperIndex: _this.data.swiperIndex
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    let swiperImageArr = options.swiperImageStr.split(',');
    this.setData({
      swiperImageArr: swiperImageArr,
      num: 8 - swiperImageArr.length,
      swiperIndex: options.swiperIndex,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // wx.showModal({
    //   title: '',
    //   content: '退出此次编辑？',
    //   showCancel: true,
    //   confirmText: '退出',
    //   success: function (res) {
    //     if (res.confirm) {
    //       console.log('11');
    //     }
    //   },
    // })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})